package com.dingding.dao.usermangerdao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.bean.databean.request.ReqCoupon;
import com.dingding.bean.databean.request.ReqUserLoginByNum;
import com.dingding.bean.databean.request.ReqUserRegister;
import com.dingding.bean.databean.response.ResCoupon;
import com.dingding.bean.databean.response.ResGetUserWallet;
import com.dingding.bean.entity.Useraccount;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.dao.basedao.impl.BaseDaoImpl;
import com.dingding.dao.sql.UserAccountServiceSQL;
import com.dingding.dao.usermangerdao.IUserAccountDao;

/**
 * 
 * @ClassName: UserAccountServiceImpl
 * @Description:用户账户Service实现类
 * @author zhaochenguang
 * @date 2015-10-20 上午09:52:25
 * @version 叮叮搭车V2.0
 */
public class UserAccountDaoImpl extends BaseDaoImpl<Useraccount> implements IUserAccountDao {

	/**
	 * @Title: saveUserAccount @Description: 保存到用户账户表 @param reqUserRegister
	 *         -用户注册请求bean @param userAccountId -账户id @param userLoginId
	 *         -登录id @param @return @author Li Yi @return @throws
	 */
	public void saveUserAccount(ReqUserRegister reqUserRegister, String userAccountId, String userLoginId) {

		Useraccount useraccount = new Useraccount();
		// 为用户账户表设置主键
		useraccount.setUaId(userAccountId);
		// 用户登录ID
		useraccount.setLoginId(userLoginId);
		// 账户余额
		useraccount.setCurrencyAmount(new BigDecimal(0));
		// 账户状态
		useraccount.setStatus(1);
		// 累计收入
		useraccount.setTotalIncome(new BigDecimal(0));
		// 累计提现
		useraccount.setTotalDeposit(new BigDecimal(0));
		// 累计支出
		useraccount.setTotalExpend(new BigDecimal(0));
		// 向用户账户表插入一条记录
		super.save(useraccount);

	}

	/**
	 * @Title: setResGetUserWallet @Description: 设置响应bean的字段 @param obj
	 *         数据库查询出来的Object[]数组 @param resUserWallet 响应信息封装的bean @author
	 *         zhaochenguang @return boolean 操作是否成功 true：成功 false：失败 @throws
	 */

	public void setResGetUserWallet(List<Object[]> obj, ResGetUserWallet resUserWallet) {

		Object[] resUserWalletObject = obj.get(0);

		// 设置返回封装bean的字段
		// 账户余额
		if (resUserWalletObject[0] != null) {
			resUserWallet.setCurrencyAmount(ToolUtil.keepOnePlacesDown(ToolUtil.castDouble(resUserWalletObject[0])));
			resUserWallet.setCurrencyAmountString(ToolUtil.keepOnePlacesDown(ToolUtil.castDouble(resUserWalletObject[0])).toString());
		}
		// 账户积分
		if (resUserWalletObject[1] != null) {
			resUserWallet.setUserIntegral(ToolUtil.castDouble(resUserWalletObject[1]));
		}
		// 累计收入
		if (resUserWalletObject[2] != null) {
			resUserWallet.setTotalIncome(ToolUtil.keepOnePlaces(ToolUtil.castDouble(resUserWalletObject[2])));
		}
		// 累计提现
		if (resUserWalletObject[3] != null) {
			resUserWallet.setTotalDeposit(ToolUtil.keepOnePlaces(ToolUtil.castDouble(resUserWalletObject[3])));
		}
		// 累计支出
		if (resUserWalletObject[4] != null) {
			resUserWallet.setTotalExpend(ToolUtil.keepOnePlaces(ToolUtil.castDouble(resUserWalletObject[4])));
		}
		// 是否设置提现密码
		if (resUserWalletObject[5] != null) {
			resUserWallet.setIsDeposit(ToolUtil.castInteger(resUserWalletObject[5]));
		}
		// 本日提现次数(每日最多提现一次)
		if (resUserWalletObject[6] != null) {
			resUserWallet.setDepositNumber(ToolUtil.castInteger(resUserWalletObject[6]));
		}
		// 优惠券数量
		if (resUserWalletObject[7] != null) {
			resUserWallet.setCouponNumber(ToolUtil.castInteger(resUserWalletObject[7]));
		}

	}

	/**
	 * @Title: getCouponList 
	 * @Description: 根据用户Id获取出行券列表
	 * @param reqCoupon
	 * @author YHQ
	 * @return List<ResCoupon>    返回类型 
	 * @throws
	 */
	@Override
	public List<ResCoupon> getCouponList(ReqCoupon reqCoupon) {
		
		//获取出行券列表
		List<Object[]> objects = super.sqlQueryListObj(UserAccountServiceSQL.GET_COUPON_LIST, false, reqCoupon.getLoginId());
		
		//判断获取的出行券列表是否为空
		if(objects != null && objects.size() > 0){
			
			//创建存储我的出行券列表的对象
			List<ResCoupon> resCoupons = new ArrayList<ResCoupon>();
			
			for (Object[] obj : objects) {
				
				resCoupons.add(objectToCoupon(obj));
			}
			
			return resCoupons;
		}
		
		return null;
	}

	/**
	 * @Title: objectToCoupon 
	 * @Description: 将Object[]类型的转为coupon类型
	 * @param 我的出行券列表 objects
	 * @author YHQ
	 * @return List<ResCoupon>    返回类型 
	 * @throws
	 */
	public ResCoupon objectToCoupon(Object[] object){
		
		//创建出行券对象
		ResCoupon resCoupon = new ResCoupon();
		
		//用户Id
		if(object[0] != null){
			resCoupon.setLoginId(object[0].toString());
		}
		//优惠金额
		if(object[1] != null){
			resCoupon.setCouponMoney(ToolUtil.castDouble(object[1]));
		}
		//有效时间
		if(object[2] != null){
			resCoupon.setExpiredTime((Date)object[2]);
		}
		//类型名称
		if(object[3] != null){
			resCoupon.setCouponName(object[3].toString());
		}
		//用户优惠券Id
		if(object[4] != null){
			resCoupon.setUcId(object[4].toString());
		}
			
		return resCoupon;
	}

	/**
	 * @Title: getResCoupon 
	 * @Description: 根据用户Id获取优惠券信息
	 * @param userId 用户Id
	 * @author YHQ
	 * @return ResCoupon    返回类型 
	 */
	@Override
	public ResCoupon getResCoupon(String userId) {
		
		//获取出行券列表
		Object[] object = super.sqlQueryUnResultObj(UserAccountServiceSQL.GET_COUPON_INFO, false, userId);
		
		//判断获取的出行券列表是否为空
		if(object != null){
			
			return objectToCouponInfo(object);
		}
		
		return null;
	}
	
	/**
	 * @Title: objectToCoupon 
	 * @Description: 将Object[]类型的转为coupon类型
	 * @param 我的出行券列表 objects
	 * @author YHQ
	 * @return List<ResCoupon>    返回类型 
	 * @throws
	 */
	public ResCoupon objectToCouponInfo(Object[] object){
		
		//创建出行券对象
		ResCoupon resCoupon = new ResCoupon();
		
		//用户优惠券Id
		if(object[0] != null){
			resCoupon.setUcId(object[0].toString());
		}
		//优惠金额
		if(object[1] != null){
			resCoupon.setCouponMoney(ToolUtil.castDouble(object[1]));
		}
			
		return resCoupon;
	}
	
	/**
	 * @Title: saveUserAccountByNum @Description: 保存到用户账户表 @param
	 *         reqUserLoginByNum -用户注册请求bean @param userAccountId -账户id @param
	 *         userLoginId -登录id @param @return @author Li Yi @return @throws
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveUserAccountByNum(ReqUserLoginByNum reqUserLoginByNum, String userAccountId, String userLoginId) {

		Useraccount useraccount = new Useraccount();
		// 为用户账户表设置主键
		useraccount.setUaId(userAccountId);
		// 用户登录ID
		useraccount.setLoginId(userLoginId);
		// 账户余额
		useraccount.setCurrencyAmount(new BigDecimal(0));
		// 账户状态
		useraccount.setStatus(1);
		// 累计收入
		useraccount.setTotalIncome(new BigDecimal(0));
		// 累计提现
		useraccount.setTotalDeposit(new BigDecimal(0));
		// 累计支出
		useraccount.setTotalExpend(new BigDecimal(0));
		// 向用户账户表插入一条记录
		super.save(useraccount);

	}
}
